package first.niuke.midding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @ClassName NC97
 * @description:
 * @author: 荣燊
 * @create: 2021-07-23 10:21
 **/
public class NC97 {
    public static void main(String[] args) {

    }

    public String[][] topKstrings (String[] strings, int k) {
        // write code here
//        HashMap<String, Integer> map = new HashMap<>();
        TreeMap<String, Integer> map = new TreeMap<>(
                new Comparator<String>() {
                    @Override
                    public int compare(String o1, String o2) {
                        return o1.compareTo(o2);
                    }
                }
        );

        for (String s : strings) {
            if (map.containsKey(s)) {
                map.put(s, map.get(s)+1);
            } else {
                map.put(s, 1);
            }
        }

        ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        String[][] strings1 = new String[k][2];
        for (int i=0; i<k; i++) {
            strings1[i][0] = entries.get(i).getKey();
            strings1[i][1] = entries.get(i).getValue().toString();
        }
        return strings1;
    }
}
